package com.crm.service.impl.returnMoney;

import com.crm.dao.mapper.customMapper.customerManagerCustomMapper.CustomerOrderBackMoneyCustomMapper;
import com.crm.dao.mapper.customMapper.returnMoneyManagerCustomMapper.ReturnMoneyCustomMapper;
import com.crm.dao.mapper.returnMoney.ReturnMoneyMapper;
import com.crm.model.dto.SalesmanDto;
import com.crm.model.entity.returnMoney.ReturnMoney;
import com.crm.model.qo.productManager.AnalyseReturnMoneyProductQO;
import com.crm.model.qo.returnMoney.ReturnMoneyQO;
import com.crm.model.vo.customerManager.CustomerOrderBackMoneyVO;
import com.crm.model.vo.orderManager.PerformanceRankVO;
import com.crm.model.vo.productManager.AnalyseReturnMoneyProductVO;
import com.crm.model.vo.productManager.PerformanceAnalysisVO;
import com.crm.model.vo.returnMoney.FinancialDetailsVO;
import com.crm.model.vo.returnMoney.PerformanceDetailsVO;
import com.crm.model.vo.returnMoney.ReturnMoneyVO;
import com.crm.service.returnMoney.ReturnMoneyDbservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ReturnMoneyDbServiceImpl implements ReturnMoneyDbservice {
    @Autowired
    private ReturnMoneyMapper returnMoneyMapper;

    @Autowired
    private CustomerOrderBackMoneyCustomMapper backMoneyCustomMapper;

    @Autowired
    private ReturnMoneyCustomMapper returnMoneyCustomMapper;

    @Override
    public List<ReturnMoneyVO> selectCustomerProductList(ReturnMoneyQO returnMoneyQO, String order,Integer dateUnit) {
        return returnMoneyMapper.selectCustomerProductList(returnMoneyQO,order,dateUnit);
    }

    /**
     * 录入回款
     * @param returnMoney
     */
    @Override
    public boolean addReturnMoney(ReturnMoney returnMoney) {
        return returnMoneyMapper.addReturnMoney(returnMoney) > 0;
    }

    @Override
    public Integer getReturnMoneyNum(String contractId) {
        return returnMoneyMapper.getReturnMoneyNum(contractId);
    }

    /**
     * 获取总公司
     */
    @Override
    public String getArriveHQCompName() {
        return returnMoneyMapper.getArriveHQCompName();
    }

    /**
     * 修改回款信息
     * @param returnMoney
     */
    @Override
    public void updateReturnMoney(ReturnMoney returnMoney) {
        returnMoneyMapper.updateReturnMoney(returnMoney);
    }

    /**
     * 根据id查询回款信息
     * @param returnMoneyId
     * @return
     */
    @Override
    public ReturnMoneyVO getReturnMoenyInfoById(String returnMoneyId) {
        return returnMoneyMapper.getReturnMoenyInfoById(returnMoneyId);
    }

    /**
     * 根据订单id修改订单信息
     * @param returnMoney
     */
    @Override
    public void updateReturnMoneyByOrderId(ReturnMoney returnMoney) {
        returnMoneyMapper.updateReturnMoneyByOrderId(returnMoney);
    }

    /**
     * 根据合同ID修改回款表的合同类型
     * @param returnMoney
     * @author yincuimei
     * @return
     */
    @Override
    public Boolean updateReturnMoneyByContractId(ReturnMoney returnMoney) {
        return returnMoneyMapper.updateReturnMoneyByContractId(returnMoney) > 0;
    }

    /**
     * 根据合同id获取回款记录
     * @param contractId
     * @return
     */
    @Override
    public List<ReturnMoney> selectReturnMoneyByContractId(String contractId) {
        List<ReturnMoney> returnMoneyList = returnMoneyMapper.selectReturnMoneyByContractId(contractId);
        return returnMoneyList;
    }

    /**
     * 根据订单id查询最后一次回款信息
     * @param productOrderId
     * @return
     */
    @Override
    public ReturnMoneyVO selectFinallyReturnMoneyByOrderId(String productOrderId) {
        return returnMoneyMapper.selectFinallyReturnMoneyByOrderId(productOrderId);
    }

    /**
     * 删除回款记录
     * @param returnMoneyId
     * @return
     */
    @Override
    public Integer delReturnMoney(String returnMoneyId,String accountId) {
        return returnMoneyMapper.delReturnMoney(returnMoneyId,accountId);
    }

    /**
     * 根据回款编号查询
     * @param arriveNum
     * @return
     */
    @Override
    public ReturnMoneyVO selectReturnMoneyByNumber(String arriveNum) {
        return returnMoneyMapper.selectReturnMoneyByNumber(arriveNum);
    }

    /**
     * 统计回款列表
     * @param returnMoneyQO
     * @return
     */
    @Override
    public List<ReturnMoneyVO> selectStatReturnMoney(ReturnMoneyQO returnMoneyQO,Integer dateUnit) {
        return returnMoneyMapper.selectStatReturnMoney(returnMoneyQO,dateUnit);
    }

    /**
     * 总部财务统计回款列表
     * @param returnMoneyQO
     * @param dateUnit
     * @return
     */
    @Override
    public List<ReturnMoneyVO> selectSuperStatReturnMoney(ReturnMoneyQO returnMoneyQO, Integer dateUnit) {
        return returnMoneyMapper.selectSuperStatReturnMoney(returnMoneyQO,dateUnit);
    }

    /**
     * 分公司财务统计回款列表
     * @param returnMoneyQO
     * @param dateUnit
     * @return
     */
    @Override
    public List<ReturnMoneyVO> selectStatBackMoney(ReturnMoneyQO returnMoneyQO, Integer dateUnit) {
        return returnMoneyMapper.selectStatBackMoney(returnMoneyQO,dateUnit);
    }

    /**
     * 总部财务统计回款列表
     * @param returnMoneyQO
     * @param dateUnit
     * @return
     */
    @Override
    public List<ReturnMoneyVO> selectSuperStatBackMoney(ReturnMoneyQO returnMoneyQO, Integer dateUnit) {
        return returnMoneyMapper.selectSuperStatBackMoney(returnMoneyQO,dateUnit);
    }

    /**
     * 通过订单id查询退款记录
     * @param orderId
     * @param
     * @return
     */
    @Override
    public List<ReturnMoneyVO> selectReturnMoneyByOrderIdAndMoney(String orderId) {
        return returnMoneyMapper.selectReturnMoneyByOrderIdAndMoney(orderId);
    }

    /**
     * 根据订单编号查询导入的回款数据
     * @param orderId
     * @return
     */
    @Override
    public ReturnMoneyVO selectImportReturnMoneyByOrderId(String orderId) {
        return returnMoneyMapper.selectImportReturnMoneyByOrderId(orderId);
    }

    /**
     * 查询已完成的退款统计信息
     * @param returnMoneyQO
     * @param dateUnit  1本周 2本月 3本年 4昨天 5今天
     * @return
     */
    @Override
    public List<CustomerOrderBackMoneyVO> selectDoneOrderBackMoney(ReturnMoneyQO returnMoneyQO, Integer dateUnit) {
        return returnMoneyMapper.selectDoneOrderBackMoney(returnMoneyQO,dateUnit);
    }

    /**
     * 查询部分回款的退款统计信息
     * @param returnMoneyQO
     * @param dateUnit  1本周 2本月 3本年 4昨天 5今天
     * @return
     */
    @Override
    public List<CustomerOrderBackMoneyVO> selectPartOrderBackMoney(ReturnMoneyQO returnMoneyQO, Integer dateUnit) {
        return returnMoneyMapper.selectPartOrderBackMoney(returnMoneyQO,dateUnit);
    }

    /**
     * 通过订单id查询回款记录
     * @param productOrderId 订单id
     * @return
     */
    @Override
    public List<ReturnMoneyVO> selectOrderId(String productOrderId) {
        return returnMoneyMapper.selectOrderId(productOrderId);
    }

    /**
     * 查询订单最后回款时间
     * @param productOrderId
     * @return
     */
    @Override
    public Date selectOrderMaxArriveTime(String productOrderId) {
        return returnMoneyMapper.selectOrderMaxArriveTime(productOrderId);
    }


    @Override
    public List<ReturnMoneyVO> testReturnMoenyList() {
        return returnMoneyMapper.testReturnMoenyList();
    }

    /**
     * 计算回款金额
     * @param companyId
     * @param dateUnit
     * @return
     */
    @Override
    public BigDecimal selectReturnMoneyCount(String companyId, Integer dateUnit) {
        return returnMoneyMapper.selectReturnMoneyCount(companyId,dateUnit);
    }

    /**
     * 计算退款金额
     * @param companyId
     * @param dateUnit
     * @return
     */
    @Override
    public BigDecimal selectBackMoneyCount(String companyId, Integer dateUnit) {
        return backMoneyCustomMapper.selectBackMoneyCount(companyId,dateUnit);
    }

    /**
     * 取最近一条的到款时间
     * @param productOrderId 订单id
     * @return
     */
    @Override
    public ReturnMoneyVO selectReturnMoneyByOrderId(String productOrderId) {
        return returnMoneyMapper.selectReturnMoneyByOrderId(productOrderId);
    }

    /**
     * 根据id查询
     * @param returnMoneyId
     * @return
     */
    @Override
    public ReturnMoneyVO selectReturnMoneyById(String returnMoneyId) {
        return returnMoneyMapper.getReturnMoenyInfoById(returnMoneyId);
    }

    /**
     * 查询订单产品回款情况
     * @param finishOrderProductId
     * @param orderId
     * @return
     */
    @Override
    public BigDecimal selectOrderProductReturnMoneyCount(String finishOrderProductId, String orderId) {
        return returnMoneyMapper.selectOrderProductReturnMoneyCount(orderId,finishOrderProductId);
    }

    /**
     * 通过产品号查询回款记录
     * @param finishOrderProductNum
     * @return
     */
    @Override
    public List<ReturnMoney> selectFinishOrderProductNum(String finishOrderProductNum) {
        List<ReturnMoney> list = returnMoneyMapper.selectFinishOrderProductNum(finishOrderProductNum);
        return list;
    }

    /**
     * 通过订单id查询回款记录
     * @param productOrderId 订单id
     * @return
     */
    @Override
    public List<ReturnMoney> selectMoneyByOrderId(String productOrderId) {
        List<ReturnMoney> list = returnMoneyMapper.selectMoneyByOrderId(productOrderId);
        return list;
    }

    @Override
    public void delFinishByOrderId(String productOrderId) {
        returnMoneyMapper.delFinishByOrderId(productOrderId);
    }

    @Override
    public BigDecimal selectReturnMoneyCountByOrderId(String orderId) {
        return returnMoneyMapper.selectReturnMoneyCountByOrderId(orderId);
    }

    /**
     * 根据退款时间更新到款时间
     * @return
     */
    @Override
    public List<ReturnMoney> selectUpdateReturnTime() {
        return returnMoneyMapper.selectUpdateReturnTime();
    }

    /**
     * 根据主键Id修改提成发放状态
     * @Author pym
     * @Date  2021/9/24 10:10
     * @param returnMoneyId 回款记录表主键
     * @param royaltyStatus 提成发放状态
     * @param accountId 更新人
     * @param updateTm 更新时间
     * @return {@link {java.lang.Boolean}}
     */
    @Override
    public Boolean updateRoyaltyStatusByReturnMoneyId(List<String> returnMoneyId, int royaltyStatus, String accountId, Date updateTm) {
        return returnMoneyCustomMapper.updateRoyaltyStatusByReturnMoneyId(returnMoneyId, royaltyStatus, accountId, updateTm) > 0;
    }

    /**
     * 通过订单id和产品号查询回款信息
     * @param productOrderId 订单id
     * @param finishOrderProductNum 产品号
     * @return
     */
    @Override
    public List<ReturnMoney> selectMoneyByOrderIdAndFinshNum(String productOrderId, String finishOrderProductNum) {
        return returnMoneyMapper.selectMoneyByOrderIdAndFinshNum(productOrderId,finishOrderProductNum);
    }

    /**
     * 财务明细
     * @return
     */
    @Override
    public List<FinancialDetailsVO> selectFinancialDetails(ReturnMoneyQO returnMoneyQO) {
        return returnMoneyMapper.selectFinancialDetails(returnMoneyQO);
    }

    /**
     * 业绩明细
     * @return
     */
    @Override
    public List<PerformanceDetailsVO> selectPerformanceDetails(ReturnMoneyQO returnMoneyQO) {
        return returnMoneyMapper.selectPerformanceDetails(returnMoneyQO);
    }

    /**
     * 按照月份统计个人的业绩金额
     * @param returnMoneyQO
     * @return
     */
    @Override
    public BigDecimal sumCustomerPay(ReturnMoneyQO returnMoneyQO) {
        return returnMoneyMapper.sumCustomerPay(returnMoneyQO);
    }

    /**
     * 按照月份统计个人的returnMoney金额
     * @param returnMoneyQO
     * @return
     */
    @Override
    public BigDecimal sumReturnScoreMoney(ReturnMoneyQO returnMoneyQO) {
        return returnMoneyMapper.sumReturnScoreMoney(returnMoneyQO);
    }

    /**
     * 删除订单下的所有回款记录
     * @param accountId
     */
    @Override
    public void delReturnMoneyByOrderId(String productOrderId,String accountId) {
        returnMoneyMapper.delReturnMoneyByOrderId(productOrderId,accountId);
    }

    @Override
    public List<ReturnMoney> selectReturnMoneyNoCustomer(String companyId) {
        return returnMoneyCustomMapper.selectReturnMoneyNoCustomer(companyId);
    }

    /**
     * 批量补充回款表客户信息(临时用补充正式库数据:customer_id;customer_name为空的)
     * @param returnMoneyList
     * @author yincuimei
     * @Date(2021-9-26)
     * @return
     */
    @Override
    public Boolean batchUpdateReturnMoneyByOrderNumber(List<ReturnMoney> returnMoneyList) {
        return returnMoneyCustomMapper.batchUpdateReturnMoneyByOrderNumber(returnMoneyList) > 0;
    }

    /**
     * 报表统计 - 业绩分析 - 销售产品分析
     * @Author pym
     * @Date  2021/9/27 14:48
     * @param analyseReturnMoneyProductQO -查询条件
     * @param order - 排序
     * @param accountId - 账号Id
     * @return {@link {java.util.List<com.crm.model.vo.productManager.AnalyseReturnMoneyProductVO>}}
     */
    @Override
    public List<AnalyseReturnMoneyProductVO> selectReturnMoneyProductList(AnalyseReturnMoneyProductQO analyseReturnMoneyProductQO, String order, String accountId) {
        return returnMoneyCustomMapper.selectReturnMoneyProductList(analyseReturnMoneyProductQO, order, accountId);
    }

    /**
     * 会员产品数量和销售额度
     * @Author pym
     * @Date  2021/10/21 10:05
     * @param accountId 账号Id
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return {@link {com.crm.model.vo.productManager.PerformanceAnalysisVO}}
     */
    @Override
    public PerformanceAnalysisVO selectMemberProductCountAndPrice(String accountId, String startDate, String endDate) {
        return returnMoneyCustomMapper.selectMemberProductCountAndPrice(accountId, startDate, endDate);
    }

    /**
     * 非会员产品数量和销售额度
     * @Author pym
     * @Date  2021/10/21 10:05
     * @param accountId 账号Id
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return {@link {com.crm.model.vo.productManager.PerformanceAnalysisVO}}
     */
    @Override
    public PerformanceAnalysisVO selectNonMemberProductCountAndPrice(String accountId, String startDate, String endDate) {
        return returnMoneyCustomMapper.selectNonMemberProductCountAndPrice(accountId, startDate, endDate);
    }

    /**
     * 年度会员产品数量和销售额度
     * @Author pym
     * @Date  2021/10/21 10:06
     * @param accountId 账号Id
     * @param year 年份
     * @return {@link {java.util.List<com.crm.model.vo.productManager.PerformanceAnalysisVO>}}
     */
    @Override
    public List<PerformanceAnalysisVO> selectYearMemberProductCountAndPrice(String accountId, Date year) {
        return returnMoneyCustomMapper.selectYearMemberProductCountAndPrice(accountId, year);
    }

    /**
     * 年度非会员产品数量和销售额度
     * @Author pym
     * @Date  2021/10/21 10:06
     * @param accountId 账号Id
     * @param year 年份
     * @return {@link {java.util.List<com.crm.model.vo.productManager.PerformanceAnalysisVO>}}
     */
    @Override
    public List<PerformanceAnalysisVO> selectYearNonMemberProductCountAndPrice(String accountId, Date year) {
        return returnMoneyCustomMapper.selectYearNonMemberProductCountAndPrice(accountId, year);
    }

    /**
     * 通过finish查询回款信息
     * @param finishId
     * @return
     */
    @Override
    public List<ReturnMoneyVO> selectReturnMoneyByFinishId(String finishId) {
        return returnMoneyCustomMapper.selectReturnMoneyByFinishId(finishId);
    }

    /**
     * 查询订单的附加费用回款金额
     * @param productOrderId
     * @return
     */
    @Override
    public BigDecimal selectOrderSurchargeCount(String productOrderId) {
        return returnMoneyCustomMapper.selectOrderSurchargeCount(productOrderId);
    }

    /**
     * 通过产品号查询回款记录
     * @param finishOrderProductNum 产品号
     * @return
     */
    @Override
    public ReturnMoney selectReturnMoneyByFinishNum(String finishOrderProductNum) {
        return returnMoneyCustomMapper.selectReturnMoneyByFinishNum(finishOrderProductNum);
    }

    /**
     * 双十一报表
     * @param entryArea
     * @param month
     * @return
     */
    @Override
    public List<PerformanceRankVO> reportForNovember(String entryArea, String year, String month,String day){
        return returnMoneyCustomMapper.reportForNovember(entryArea,year,month,day);
    }

    @Override
    public ReturnMoney selectLastByOrderId(String orderId, String orderBy) {
        return returnMoneyCustomMapper.selectLastByOrderId(orderId, orderBy);
    }

    @Override
    public ReturnMoney selectLastByFinishOrderProductNum(String finishOrderProductNum) {
        return returnMoneyCustomMapper.selectLastByFinishOrderProductNum(finishOrderProductNum);
    }

    @Override
    public ReturnMoney getNoCloseOne(String productOrderId) {
        return returnMoneyCustomMapper.getNoCloseOne(productOrderId);
    }

    @Override
    public ReturnMoney getCloseOne(String productOrderId) {
        return returnMoneyCustomMapper.getCloseOne(productOrderId);
    }

    @Override
    public List<ReturnMoneyVO> selectListByOrderId(String orderId) {
        return returnMoneyCustomMapper.selectListByOrderId(orderId);
    }

    /**
     * 统计业务员每月的业绩
     */
    @Override
    public List<SalesmanDto> getSalesmanResultsByMonth() {
        return returnMoneyMapper.getSalesmanResultsByMonth();
    }

    /**
     * 统计业务员每年的业绩
     */
    @Override
    public List<SalesmanDto> getSalesmanResultsByYear() {
        return returnMoneyMapper.getSalesmanResultsByYear();
    }

    @Override
    public BigDecimal queryPreReturnMoneyTotal(String orderId,String finishOrderProductNum) {
        return returnMoneyCustomMapper.queryPreRetunmoneyTotal(orderId,finishOrderProductNum);
    }

    /**
     * 查询产品回款金额含预回款
     * @param finishOrderProductNum
     * @return
     */
    @Override
    public BigDecimal selectOrderProductReturnMoneyAllCount(String finishOrderProductNum,String orderId) {
        return returnMoneyCustomMapper.selectOrderProductReturnMoneyAllCount(finishOrderProductNum,orderId);
    }
}
